package com.itheima.dao;

import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CheckGroupDao {

    @Insert("insert into t_checkgroup(id,code,name,helpCode,sex,remark,attention)" +
            " values (#{id},#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public Integer add(CheckGroup checkGroup);

    @Insert("insert into t_checkgroup_checkitem (checkgroup_id,checkitem_id) values(#{checkGroupId},#{checkItemId})")
    public Integer addCheckGroupIdCheckAndCheckItemId(@Param("checkGroupId") Integer checkGroupId,@Param("checkItemId") Integer checkItemId);

    @Select("select * from t_checkgroup where code like CONCAT('%',#{queryString},'%') or name like CONCAT('%',#{queryString},'%') or helpCode like CONCAT('%',#{queryString},'%')")
    public List<CheckGroup> findPage(String queryString);

    @Select("select * from t_checkgroup")
    public List<CheckGroup> findPageAll();

    @Select("select id,code,name,helpCode,sex,remark,attention from t_checkgroup t where id=#{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "code",property = "code"),
            @Result(column = "name",property = "name"),
            @Result(column = "helpCode",property = "helpCode"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "remark",property = "remark"),
            @Result(column = "attention",property = "attention"),
            @Result(column = "id",property="checkItems",
            javaType = java.util.List.class,
            many=@Many(select = "com.itheima.dao.CheckItemDao.findAllCheckItemAndCheckGroup1"))
    })
    public CheckGroup findById(Integer id);

    @Select("select checkitem_id from  t_checkgroup_checkitem where checkgroup_id=#{checkGroupId}")
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId);

    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention},remark=#{remark} where id=#{id}")
    public Integer update(CheckGroup checkGroup);

    @Delete("delete from t_checkgroup_checkitem where checkgroup_id=#{checkGroupId}")
    public Integer delete(Integer checkGroupId);

    @Insert("insert into t_checkgroup_checkitem (checkgroup_id,checkitem_id) values(#{checkGroupId},#{checkItemId})")
    public Integer addCheckGroupIdAndCheckItem(@Param("checkGroupId") Integer checkGroupId,@Param("checkItemId") Integer checkItemId);

    @Delete("delete from t_checkgroup where id=#{id}")
    public Integer deleteCheckGroup(Integer id);


    //根据套餐 id ，查询 检查组信息
    // private List<CheckItem> checkItems;//一个检查组合包含多个检查项
    @Select("select id,code,name,helpCode,sex,remark,attention from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{SetmealId})")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "code",property = "code"),
            @Result(column = "name",property = "name"),
            @Result(column = "helpCode",property = "helpCode"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "remark",property = "remark"),
            @Result(column = "attention",property = "attention"),
            @Result(column = "id",property = "checkItems",javaType = java.util.List.class,
            many = @Many(select = "com.itheima.dao.CheckItemDao.findByCheckGroupId")),
    })
    public List<CheckGroup> findBySetmealId(Integer SetmealId);

}

